// @Author EthanScriptOn
// @Desc
package config_processor_management

import (
	"errors"
	"gitee.com/fatzeng/srf_switch_basic_components/initialization"
	"gitee.com/fatzeng/srf_switch_basic_components/processor"
	"gitee.com/fatzeng/srf_switch_basic_components/resource"
	"gitee.com/fatzeng/srf_switch_config/config_define"
	"math"
)

var parseProcessorManager = func() *initialization.OnceInitializer {
	initializerInstance := initialization.GenerateOnceInitializer(config_define.ParserPositionTag, config_define.ParseProcessorManagerInitTag)
	if err := initializerInstance.Set(math.MaxInt, func() (result interface{}, err error) {
		information, err := GetProcessorManagerIdentity()
		if err != nil {
			return nil, err
		}
		return &SimpleParseProcessorManager{
			resourceManagerUpGradationInstance: resource.GenerateResourceManagerUpGradationMultiLevel(),
			resourceProcessorEntityIdentity:    information,
			isCover:                            true,
		}, nil
	}); err != nil {
		panic(err)
	}
	return initializerInstance
}()

func GetParseProcessorManager() (processor.ProcessorManager, error) {
	processorManagerInstance, err := parseProcessorManager.Get()
	if err != nil {
		return nil, err
	}
	processorManagerConversionInstance, ok := processorManagerInstance.(processor.ProcessorManager)
	if !ok {
		return nil, errors.New("processor resource type error need ProcessorManager")
	}
	return processorManagerConversionInstance, nil
}

const FreightIdentifier = "FreightIdentifier"

var freightProcessorManager = func() *initialization.OnceInitializer {
	initializerInstance := initialization.GenerateOnceInitializer(config_define.ParserPositionTag, config_define.FreightProcessorManagerInitTag)
	if err := initializerInstance.Set(math.MaxInt, func() (result interface{}, err error) {
		return &SimpleFreightProcessorManager{
			resourceManager: resource.GenerateResourceManager(true),
		}, nil
	}); err != nil {
		panic(err)
	}
	return initializerInstance
}()

func GetFreightProcessorManager() (processor.ProcessorManager, error) {
	processorManagerInstance, err := freightProcessorManager.Get()
	if err != nil {
		return nil, err
	}
	processorManagerConversionInstance, ok := processorManagerInstance.(processor.ProcessorManager)
	if !ok {
		return nil, errors.New("processor resource type error need ProcessorManager")
	}
	return processorManagerConversionInstance, nil
}
